﻿namespace QuickMail.BusinessLogic
{
    using System;
    using System.IO;
    using System.Xml.Serialization;
    using QuickMail.DataModels;

    public static class XmlWorker
    {
        /// <summary>
        /// Serializations language file.
        /// </summary>
        public static void SerializeLang(string path, LanguageXml languageXml)
        {
            try
            {
                using (var fileStream = new FileStream(path, FileMode.Create))
                {
                    var serialize = new XmlSerializer(typeof(LanguageXml));
                    serialize.Serialize(fileStream, languageXml);
                }
            }
            catch (Exception ex)
            {
                StaticFields.Logger.Warn(" -- {0}.{1} -- {2}", typeof(EnvironmentWorker).FullName,
                    System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message);
            }
        }

        /// <summary>
        /// Deserializes the selected language file.
        /// </summary>
        public static LanguageXml DeserializeLang(string path)
        {
            try
            {
                using (var fileStream = new FileStream(path, FileMode.Open))
                {
                    var serializer = new XmlSerializer(typeof(LanguageXml));
                    return (LanguageXml)serializer.Deserialize(fileStream);
                }
            }
            catch (Exception ex)
            {
                StaticFields.Logger.Warn(" -- {0}.{1} -- {2}", typeof(EnvironmentWorker).FullName,
                    System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message);
                return new LanguageXml();
            }
        }
    }
}
